package com.uniflow.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;

/**
 * 用户权限实体类（用于特殊权限分配）
 * 
 * @author luban
 * @since 1.0.0
 */
@ApiModel(description = "用户权限")
public class UserPermission {
    
    @ApiModelProperty("关联ID")
    private String id;
    
    @ApiModelProperty("用户ID")
    private String userId;
    
    @ApiModelProperty("权限ID")
    private String permissionId;
    
    @ApiModelProperty("是否授权：1-授权，0-拒绝")
    private Boolean granted;
    
    @ApiModelProperty("授权人ID")
    private String grantedBy;
    
    @ApiModelProperty("授权人姓名")
    private String grantedByName;
    
    @ApiModelProperty("授权时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime grantedAt;
    
    @ApiModelProperty("过期时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime expiresAt;
    
    @ApiModelProperty("授权原因")
    private String reason;
    
    @ApiModelProperty("是否临时权限")
    private Boolean temporary;
    
    // 关联查询字段
    @ApiModelProperty("用户信息")
    private User user;
    
    @ApiModelProperty("权限信息")
    private Permission permission;
    
    @ApiModelProperty("授权人信息")
    private User granter;
    
    // 构造函数
    public UserPermission() {}
    
    public UserPermission(String userId, String permissionId) {
        this.userId = userId;
        this.permissionId = permissionId;
        this.granted = true;
        this.grantedAt = LocalDateTime.now();
        this.temporary = false;
    }
    
    public UserPermission(String userId, String permissionId, Boolean granted, String grantedBy, String reason) {
        this.userId = userId;
        this.permissionId = permissionId;
        this.granted = granted;
        this.grantedBy = grantedBy;
        this.reason = reason;
        this.grantedAt = LocalDateTime.now();
        this.temporary = false;
    }
    
    // 便利方法
    public boolean isExpired() {
        return expiresAt != null && LocalDateTime.now().isAfter(expiresAt);
    }
    
    public boolean isValid() {
        return granted != null && granted && !isExpired();
    }
    
    // Getter和Setter方法
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getUserId() {
        return userId;
    }
    
    public void setUserId(String userId) {
        this.userId = userId;
    }
    
    public String getPermissionId() {
        return permissionId;
    }
    
    public void setPermissionId(String permissionId) {
        this.permissionId = permissionId;
    }
    
    public Boolean getGranted() {
        return granted;
    }
    
    public void setGranted(Boolean granted) {
        this.granted = granted;
    }
    
    public String getGrantedBy() {
        return grantedBy;
    }
    
    public void setGrantedBy(String grantedBy) {
        this.grantedBy = grantedBy;
    }
    
    public String getGrantedByName() {
        return grantedByName;
    }
    
    public void setGrantedByName(String grantedByName) {
        this.grantedByName = grantedByName;
    }
    
    public LocalDateTime getGrantedAt() {
        return grantedAt;
    }
    
    public void setGrantedAt(LocalDateTime grantedAt) {
        this.grantedAt = grantedAt;
    }
    
    public LocalDateTime getExpiresAt() {
        return expiresAt;
    }
    
    public void setExpiresAt(LocalDateTime expiresAt) {
        this.expiresAt = expiresAt;
    }
    
    public String getReason() {
        return reason;
    }
    
    public void setReason(String reason) {
        this.reason = reason;
    }
    
    public Boolean getTemporary() {
        return temporary;
    }
    
    public void setTemporary(Boolean temporary) {
        this.temporary = temporary;
    }
    
    public User getUser() {
        return user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }
    
    public Permission getPermission() {
        return permission;
    }
    
    public void setPermission(Permission permission) {
        this.permission = permission;
    }
    
    public User getGranter() {
        return granter;
    }
    
    public void setGranter(User granter) {
        this.granter = granter;
    }
    
    @Override
    public String toString() {
        return "UserPermission{" +
                "id='" + id + '\'' +
                ", userId='" + userId + '\'' +
                ", permissionId='" + permissionId + '\'' +
                ", granted=" + granted +
                ", grantedAt=" + grantedAt +
                ", expiresAt=" + expiresAt +
                ", temporary=" + temporary +
                '}';
    }
}